#!/bin/bash

# 镜像不存在时创建镜像
if ! docker images | grep essay; then
    echo 'The docker image does not exist,'
    echo 'Now creating image <essay>...'
    docker build -t essay $(pwd)
fi

# 镜像存在时，检查容器是否存在
if docker ps -a | grep -i webapp-essay; then
    # 容器存在时则删除容器
    echo 'The docker container <essay> already exist, deleting it...'
    docker rm -f webapp-essay
fi

# 启动容器
docker run -itd \
           --link mysql-essay:mysql \
           -v ~/git_repo/Note/LifeEssays:/home/docker/code/LifeEssays \
           -v ~/media:/root/media \
           --name webapp-essay \
           -p 9000:80 \
       essay \
       sh -c 'python3 /home/docker/code/LifeEssays/manage.py collectstatic --noinput && supervisord -n'